define(['butterfly/view'], function(View){

  //show only contain one direct subview
  return View.extend({

    initialize: function(options){
      View.prototype.initialize.apply(this, arguments);
      this.views = [];
      this.currentView = '';
      this.baseZIndex = 10;
      this.firstLoad = true;
    },

    route: function(paths, options){
//console.log("")
	    if(!paths){//地址栏没有配置路径
		    console.log("paths没数据")
		    /* ========================================================================
		     * 通过index.html中的data-default定义默认跳转
		     * 若data-default不存在或为空，调到当前模块的main.html
		     * ======================================================================== */
		    var mainEl,
			    defaultPath,
			    defaultModule;

		    mainEl = $(this.el);

		    //配置路径
		    defaultPath = mainEl.attr('data-default');

		    //获取当前模块
		    var pathname = window.location.pathname;
		    var rootPath = pathname.substr(0, pathname.lastIndexOf('/'));
		    defaultModule = rootPath.substr(rootPath.lastIndexOf('/')+1);

		    if (!defaultPath) {
			    if (defaultModule) {

				    defaultPath = defaultModule + '/main';

			    }else{//data-default 和meta当前模块定义都没有配置
				    console.log("第一个div的data-default默认路径和meta标签当前模块定义必须配置其中一个");
				    return;
			    }
		    }
		    else if (defaultPath.indexOf('/') == -1)//若data-default没有模块名，则默认为当前模块
		    {
			    defaultPath = defaultModule + '/' + defaultPath;
		    }
	    }

      var path = paths;
      if(!paths) {
        Backbone.history.navigate('#/' + defaultPath);
        path = defaultPath || "";
      }
	    //console.log(path)

      require(['view!' + path + '.html'], function(ViewClass){
        var viewsLength = this.views.length;
        //判断是不是后退操作。
        if (viewsLength > 1 && (this.views[viewsLength - 2].path === path)) {
          var currentView = this.views[this.views.length - 1].view;
          currentView.animateSlideOutRight(function(){
            //hide & remote top
            currentView.$el.hide();
            currentView.$el.remove();
            //show next
            this.views[this.views.length - 2].view.show();
            this.views.pop();
            this.baseZIndex--;
            console.log(this.views)
          }.bind(this));
          
        } else {
          var newView = new ViewClass();
          this.views.push({path: path, view: newView});
          console.log(this.views)
          newView.$el.css({
            'position': 'absolute',
            'top': '0px',
            'bottom': '0px',
            'width': '100%',
            'z-index': this.baseZIndex++
          });
          newView.render();
          this.el.appendChild(newView.el);
          newView.show(options);

          if (!this.firstLoad) newView.animateSlideInRight();
        }

        this.firstLoad = false;
        // this.hideLoading();//删除loading层
      }.bind(this), function(err){
        Backbone.history.navigate('#/' + path);
      });


    }
  });
});
